/*globals TJ, jQuery */
(function ($) {
    /**
     * Basic module structure
     * @class Module
     */
    function Module() {
        var self = this,
            connectors_string = '',
            main = null;
        
        this.$inner = null;
        this.$bd = null;
        this.$ft = null;
        this.$hd = null;

        this.connectors = [];

        this.set_dom = function ($ctx) {
            self.$inner = $('.inner:first', $ctx);
            self.$bd = $('.bd:first', $ctx);
        };

        this.init = function ($ctx) {

            if ($ctx.length !== 1) {
                throw new Error('Invalid context');
            }

            var mod_class_name = '',
                options = null;

            mod_class_name = $ctx.context.classList[1];

            (function () {
                var connectors_arr_string = [],
                    i = 0,
                    j = 0;

                for (i = 0, j = $ctx.context.classList.length; i < j; i++) {
                    if ($ctx.context.classList[i].indexOf('con') === 0) {
                        self.connectors.push($ctx.context.classList[i]);
                    }
                }

                for (i = 0, j = self.connectors.length; i < j; i++) {
                    //connectors_arr_string.push('.' + self.connectors[i]);
                    connectors_arr_string.push('.' + self.connectors[i] + ':not(.' + mod_class_name + ')');
                }

                connectors_string = connectors_arr_string.join(',');

             }());

            self.get_connectors_context = function () {
                
                return $(connectors_string);
            };
            
            if (arguments.length > 1) {
                options = arguments[1];
            }

            if (options !== null && $.isFunction(options.beforeload)) {
                options.beforeload();
            }

            $ctx.addClass(mod_class_name + 'Loading');

            self.set_dom($ctx);

            self.main($ctx, function () {
                $ctx.removeClass(mod_class_name + 'Loading');
                $ctx.addClass(mod_class_name + 'Loaded');

                if (options !== null && $.isFunction(options.afterload)) {
                    options.afterload();
                }
            });


        };
    }

    TJ.Module = Module;
}(jQuery));